package com.lanou.tk.tourism.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.lanou.tk.tourism.R;
import com.lanou.tk.tourism.entity.Total;
import com.lanou.tk.tourism.tool.GetBitmaps;

import java.util.List;


/**
 * Created by dllo on 15/10/29.
 */
public class RecommendTotalAdapter extends RecyclerView.Adapter<RecommendTotalAdapter.LatestViewHolder> {

    private List<Total> totals;
    private RequestQueue queue;


    public RecommendTotalAdapter(Context c) {
        queue = Volley.newRequestQueue(c);

    }


    public void addData(List<Total> datas) {
        totals = datas;
        notifyDataSetChanged();
    }


    @Override
    public void onBindViewHolder(final LatestViewHolder holder, int position) {
        if (totals != null && totals.size() > 0) {
            Total data = totals.get(position);

            String imgUrl = data.getPhoto();
            //网络拉取图片并显示
            GetBitmaps.getBitmapRequest(imgUrl, holder.total_Iv);


        }
        // 如果设置了回调，则设置点击事件
        if (mOnItemClickLitener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView, pos);
                }
            });
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getPosition();
                    mOnItemClickLitener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }



    public interface OnItemClickLitener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }



    @Override
    public int getItemCount() {
        return totals != null && totals.size() > 0 ? totals.size() : 0;

    }

    public class LatestViewHolder extends RecyclerView.ViewHolder {
        private ImageView total_Iv;

        public LatestViewHolder(View itemView) {
            super(itemView);

            total_Iv = (ImageView) itemView.findViewById(R.id.total_Iv);


        }
    }

    @Override
    public LatestViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {

        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());

        View v = inflater.inflate(R.layout.adapter_recommend_total, null);
        return new LatestViewHolder(v);
    }


}

